package com.woniuxy.qiantai.mq;

import com.rabbitmq.client.Channel;
import com.woniuxy.qiantai.mapper.OrderMapper;
import com.woniuxy.qiantai.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
//商家死信处理
@Component
public class MercantileDeadListener {
    @Autowired
    OrderService orderService;
    @Autowired
    OrderMapper orderMapper;

    //处理死信队列
    @RabbitListener(queues = "MercantileDeadQueue")
    public void detailDeadMessage(Message message, Channel channel, Integer orderId) throws IOException {
        //自动ack的时候,如果方法正常执行,就确认消息被消费
        //获取用户订单状态
        Integer status = orderMapper.selectById(orderId).getStatus();
        System.out.println("商家判断用户当前订单状态："+status);

        //判断商家是否接单
        if (status==3){
            try {
                System.out.println("商家接单超时已取消"+orderId+"订单");
                orderService.updateOrderState(orderId,5);

                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            System.out.println(orderId+"商家已接单");
        }
        //自动ack的时候,监听方法里不要抛出异常,否则会无限重试,导致死循环
        channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
        //channel.basicConsume(MqDelayConfig.DEAD_QUEUE,false,detailDeadMessage();)


    }
}
